home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / arc521.lha / arc / minix / mktime.c < prev    next >
C/C++ Source or Header  |  1989-08-08  |  1KB  |  72 lines

  1. /*
  2.  * mktime.c
  3.  *    ++jrb
  4.  */
  5.  
  6. #include <sys/types.h>
  7. #include <time.h>
  8.  
  9. /*
  10.  * days in a given year
  11.  */
  12. #define days_in_year(Y) (leap(Y) ? 366 : 365)
  13.  
  14. /* # of days / month in a normal year */
  15. static unsigned int md[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  16.  
  17. static int leap (y)
  18. int y;
  19. {
  20.     y += 1900;
  21.     if ((y % 400) == 0)
  22.         return (1);
  23.     if ((y % 100) == 0)
  24.         return (0);
  25.     return ((y % 4) == 0);
  26. }
  27.  
  28. /* Return the number of days between Jan 1, Base Year and the given
  29.  * broken-down time.
  30.  */
  31.  
  32. static unsigned long ndays (since, year, month, day)
  33. unsigned int year, month, day;
  34. {
  35.     register unsigned long n = day;
  36.     register unsigned int m, y;
  37.     
  38.     for (y = since; y < year; y++)
  39.     {
  40.         n += 365;
  41.         if (leap (y)) n++;
  42.     }
  43.     for (m = 0; m < month; m++)
  44.         n += md[m] + ( ((m == 1) && leap(y))? 1 : 0);
  45.  
  46.     return (n);
  47. }
  48.  
  49. /* Convert a broken-down time into seconds
  50.  *
  51.  */
  52.  
  53. static 
  54. time_t tm_to_time (base_year, year, month, day, hours, mins, secs)
  55. unsigned int base_year, year, month, day, hours, mins, secs;
  56. {
  57.     register time_t t;
  58.     extern unsigned long ndays();
  59.     
  60.     t = (ndays(base_year, year, month, day) - 1L) * (unsigned long)86400L
  61.         + hours * (unsigned long)3600L + mins * (unsigned long)60L + secs;
  62.  
  63.     return t;
  64. }
  65.  
  66. time_t mktime(tmptr)
  67. struct tm *tmptr;
  68. {
  69.     return tm_to_time(70, tmptr->tm_year, tmptr->tm_mon, tmptr->tm_mday,
  70.               tmptr->tm_hour, tmptr->tm_min, tmptr->tm_sec);
  71. }
  72.